/**
 * @program: LeetCode
 * @description: LeetCode :剑指 Offer II 008. 和大于等于 target 的最短子数组
 * @author: WXY
 * @create: 2022-12-11 21:41
 * @Version 1.0
 **/


public class Num_offer2_008_minSubArrayLen {
    public static int minSubArrayLen(int target, int[] nums) {
        if (nums.length == 1 && nums[0] == target) return 1;
        if (nums.length == 1 && nums[0] != target) return 0;
        int L = 0;
        int R = 0;
        int minLen = 0;
        boolean one = false;
        int sum = nums[L];
        while (R < nums.length) {
            if (sum < target) {
                R++;
                if (R == nums.length) break;
                sum = sum + nums[R];
            } else {
                sum = sum - nums[L];
                int Len = R - L + 1;
                if (one == false) {
                    minLen = Len;
                    one = true;
                } else {
                    minLen = Math.min(minLen, Len);
                }
                L++;
            }

        }
        return minLen;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{10, 2, 3};
        int i = minSubArrayLen(6, arr);
        System.out.println(i);
    }

}
